﻿using System;
using System.Data;
using System.Data.Common;

namespace Gumini.JulaDB.Provider
{
    /// <summary>
    /// This is a DataAdapter implementation for JulaDB. Almost all functionality is inherited from DbDataAdapter.
    /// </summary>
    [System.ComponentModel.DesignerCategory("")]
    public class JulaDbDataAdapter : DbDataAdapter
    {
        // these object are to support Component event mechanism
        private static readonly object EventRowUpdated = new object();
        private static readonly object EventRowUpdating = new object();

        // implementing 4 constructors recommended by MSDN
        #region Constructors

        public JulaDbDataAdapter() { }
        public JulaDbDataAdapter(JulaDbCommand selectCommand)
		{
            SelectCommand = selectCommand;
		}
        public JulaDbDataAdapter(String selectCommandText, JulaDbConnection selectConnection)
            : this(new JulaDbCommand(selectCommandText, selectConnection)) { }
        public JulaDbDataAdapter(String selectCommandText, String selectConnectionString)
            : this(selectCommandText, new JulaDbConnection(selectConnectionString)) { }
        
        #endregion

        public new JulaDbCommand SelectCommand
        {
            get { return (JulaDbCommand)base.SelectCommand; }
            set { base.SelectCommand = value; }
        }

        public new JulaDbCommand InsertCommand
        {
            get { return (JulaDbCommand)base.InsertCommand; }
            set { base.InsertCommand = value; }
        }

        public new JulaDbCommand UpdateCommand
        {
            get { return (JulaDbCommand)base.UpdateCommand; }
            set { base.UpdateCommand = value; }
        }

        public new JulaDbCommand DeleteCommand
        {
            get { return (JulaDbCommand)base.DeleteCommand; }
            set { base.DeleteCommand = value; }
        }

        protected override RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
        {
            return new JulaRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
        }

        protected override RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
        {
            return new JulaRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
        }

        protected override void OnRowUpdating(RowUpdatingEventArgs value)
        {
            var handler = (EventHandler<JulaRowUpdatingEventArgs>)Events[EventRowUpdating];
            if ((null != handler) && (value is JulaRowUpdatingEventArgs))
            {
                handler(this, (JulaRowUpdatingEventArgs)value);
            }
        }

        protected override void OnRowUpdated(RowUpdatedEventArgs value)
        {
            var handler = (EventHandler<JulaRowUpdatedEventArgs>)Events[EventRowUpdated];
            if ((null != handler) && (value is JulaRowUpdatedEventArgs))
            {
                handler(this, (JulaRowUpdatedEventArgs)value);
            }
        }

        public event EventHandler<JulaRowUpdatingEventArgs> RowUpdating
        {
            add { Events.AddHandler(EventRowUpdating, value); }
            remove { Events.RemoveHandler(EventRowUpdating, value); }
        }

        public event EventHandler<JulaRowUpdatedEventArgs> RowUpdated
        {
            add { Events.AddHandler(EventRowUpdated, value); }
            remove { Events.RemoveHandler(EventRowUpdated, value); }
        }
    }

    public class JulaRowUpdatingEventArgs : RowUpdatingEventArgs
    {
        public JulaRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
            : base(row, command, statementType, tableMapping) { }
            
        public new JulaDbCommand Command
        {
            get { return (JulaDbCommand)base.Command; }
            set { base.Command = value; }
        }
    }

    public class JulaRowUpdatedEventArgs : RowUpdatedEventArgs
    {
        public JulaRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
            : base(row, command, statementType, tableMapping) { }
        
        public new JulaDbCommand Command
        {
            get { return (JulaDbCommand)base.Command; }
        }
    }
}
